﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Weight.Base;
using Weight.Dal;
using Weight.Dal.Image;
using Weight.Model;

namespace Weight.Bll.Image
{
   public   class ImageBll
    {
        private ImageDal dal = new ImageDal();
        /// <summary>
        /// 创建图片
        /// </summary>
        /// <param name="diskNo">盘符</param>
        /// <returns></returns>
        public bool CreateImageData(string diskNo ,string WeightDbName,bool isLocalDb)
        {
            string path = string.Empty;
            if (!isLocalDb)
            {
                string remotePath = dal.GetDataBasePath(WeightDbName);
                int index = remotePath.LastIndexOf(@"\");
                if(!string.IsNullOrEmpty(remotePath) && index > 0)
                {
                    path = remotePath.Substring(0, index);
                }
            }
            else
            {
                 path = diskNo + @":\ImageDb";
            }
            
            if(!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
            }
            if (string.IsNullOrEmpty(path)) return false;
            string year = dal.GetYearFromDb();
            string dbName = "Image" +year+ServiceManager.SystemConfig.CompanyName.CompanyName ;
            if (dal.DbIsExist(dbName)) return true;
            return   dal.CreateImageData(path,dbName);
        }

        /// <summary>
        /// 图标是否已经存在
        /// </summary>
        /// <returns></returns>
        public bool ImageTableIsExist()
        {
            string year = dal.GetYearFromDb();
            return dal.ImageTableIsExist(year);
        }

        /// <summary>
        /// 创建图标
        /// </summary>
        /// <returns></returns>
        public bool CreateWeightImg()
        {
            string year = dal.GetYearFromDb();
            return dal.CreateWeightImgTable(year);
        }


        /// <summary>
        /// 保存图片
        /// </summary>
        /// <returns></returns>
        public bool InsertImage(WeightImageEntity entity)
        {
            string year = dal.GetYearFromDb();
            return dal.InsertImage(entity,year);
        }

        /// <summary>
        /// 查询图片
        /// </summary>
        /// <param name="pronum"></param>
        /// <param name="year"></param>
        /// <returns></returns>
        public Tuple<bool, WeightImageEntity,string> Get(string id)
        {
           var  weightDal = new DalWeight();
           var weightData = weightDal.GetWeightById(id,-1);
            if(weightData != null)
            {
                if (string.IsNullOrEmpty(weightData.ProNum) || string.IsNullOrEmpty(weightData.WeightDate))
                {
                    return new Tuple<bool, WeightImageEntity, string>(false, null, "参数错误");
                }
                DateTime date;
                if (DateTime.TryParse(weightData.WeightDate, out date))
                {
                    string year = date.ToString("yyyy");
                    var imgData = dal.Get(weightData.ProNum, year);
                    return new Tuple<bool, WeightImageEntity, string>(true, imgData, "");
                }
                else
                {
                    return new Tuple<bool, WeightImageEntity, string>(false, null, "参数错误");
                }
            }
            else
            {
                return new Tuple<bool, WeightImageEntity, string>(false, null, "参数错误");
            }
           
        }

    }
}
